// 基本信息
import { GatewayType } from "@/api/interface/iot";
import DialogBox from "@/components/DialogBox";
import { ElForm } from "element-plus";
import { defineComponent, PropType } from "vue";
interface DeviceForm {
	[key: string]: any;
}
export default defineComponent({
	props: {
		detail: {
			type: Object as PropType<DeviceForm>,
			default() {
				return {};
			}
		}
	},
	emits: ["submit"],
	setup(props, { attrs, emit }) {
		type FormInstance = InstanceType<typeof ElForm>;
		const formRef = ref<FormInstance>();
		const formRule = reactive({
			productModel: [{ required: true, message: "请输入产品Code", trigger: "change" }]
		});

		if (!props.detail.deviceType) {
			props.detail.deviceType = GatewayType.Device;
		}
		const validData = () => {
			formRef.value?.validate(valid => {
				if (valid) {
					emit("submit");
				}
			});
		};
		onMounted(() => {
			//默认为软产品
			if (!props.detail.type) {
			}
		});
		return () => {
			return (
				<DialogBox
					{...attrs}
					footerLabel={["关闭"]}
					onConfirm={() => {
						validData();
					}}
				>
					<el-form class="logger-detail-form" model={props.detail} ref={formRef} rules={formRule} label-width="100px">
						<el-form-item label="操作人">{props.detail.username}</el-form-item>
						<el-form-item label="API接口">{props.detail.uri}</el-form-item>
						<el-form-item label="接口类型">
							<el-tag type={props.detail.method == "GET" ? "" : "info"}>{props.detail.method}</el-tag>
						</el-form-item>
						<el-form-item label="操作结果">
							<el-tag type={props.detail.code == 200 ? "success" : "danger"}>{props.detail.code}</el-tag>
						</el-form-item>
						<el-form-item label="用户IP">{props.detail.ip}</el-form-item>
						<el-form-item label="用户地址">{props.detail.region}</el-form-item>
						<el-form-item label="接口耗时">{props.detail.latency}</el-form-item>
						<el-form-item label="操作时间">{props.detail.operateTime}</el-form-item>
						<el-form-item label="请求参数">{props.detail.param || "--"}</el-form-item>
					</el-form>
				</DialogBox>
			);
		};
	}
});
